package com.xiaoqi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoqi.dao.OrderSettingDao;
import com.xiaoqi.exception.SunnydayException;
import com.xiaoqi.pojo.OrderSetting;
import com.xiaoqi.service.OrderSettingService;
import com.xiaoqi.utils.POIUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author PC
 * creationTime: 2022/11/10 14:44
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceimpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    /**
     * 处理用户上传文件
     * @param list
     */
    @Override
    public void add(List<OrderSetting> list) throws IOException, ParseException {

        for (OrderSetting orderSetting : list) {
            //2.取出日期
            Date date = orderSetting.getOrderDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = sdf.format(date);
            //取出可预约人数
            int number = orderSetting.getNumber();
            //            根据时间查出预约设置
            OrderSetting orderSettingInDb = orderSettingDao.findByDate(dateStr);
            //如果存在就在进行判断,如果不存在就添加
            if (orderSettingInDb!=null){
                //将数据库里的以预约人数和将要修改的可预约人数对比
                if(orderSettingInDb.getReservations()>number){
                    //已预约人数大于可预约人数则抛出异常
                    throw new SunnydayException(orderSetting.getOrderDate()+"中，已预约的人数比你现在设置的可预约人数要大");
                }
                //如果不大于则修改可预约人数
                orderSettingDao.updateNumber(orderSetting);
            }else{
//                如果不存在则新增
                orderSettingDao.add(orderSetting);


            }
        }


    }

    /**
     * 通过月份查询这个月的预约情况
     * @param month
     * @return
     */
    @Override
    public List<Map<String, Object>> findByMonth(String month) {
        return orderSettingDao.findByMonth(month+"-01");
    }

    /**
     * 对传入的日期进行可预约人数的编辑
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        //1.取出日期
        Date date = orderSetting.getOrderDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);
        System.out.println(date);
        System.out.println("1===================");
        //取出可预约人数
        int number = orderSetting.getNumber();
        //根据时间查出预约设置
        OrderSetting orderSettingInDb = orderSettingDao.findByDate(dateStr);
        System.out.println(orderSettingInDb.getOrderDate());
        System.out.println("2===================");
        //如果存在就在进行判断,如果不存在就添加
        if (orderSettingInDb!=null){
            //将数据库里的以预约人数和将要修改的可预约人数对比
            if(orderSettingInDb.getReservations()>number){
                //已预约人数大于可预约人数则抛出异常
                throw new SunnydayException(orderSetting.getOrderDate()+"中，已预约的人数比你现在设置的可预约人数要大");
            }
            System.out.println("3===================");
            System.out.println(orderSetting.getNumber());
            System.out.println(orderSetting.getReservations());
            //如果不大于则修改可预约人数
            orderSettingDao.updateNumber(orderSetting);
        }else{
//                如果不存在则新增
            orderSettingDao.add(orderSetting);


        }
    }
}
